/*
 * vws_http_error.cpp
 *
 *  Created on: Sep 20, 2013
 *      Author: vscen
 */

#include "vws_http_error.h"

namespace vws {
bool vws_http_error::get_error_page(int nIndex, char* pszBuffer, size_t nSize) {
	char pszMsg[PATH_MAX] = { 0 };
	static char fmt[] =
			"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\r\n"
				"<HTML>\r\n<HEAD><TITLE>form server response</TITLE></HEAD>\r\n<BODY>\r\n"
				"<div align=\"center\"><h1>%s %s</h1><hr/>\r\n"
				"<div align=\"center\"><h4>Generate:dTcRaCkEr/0.01.<h4>\r\n"
				"</BODY>\r\n</HTML>";
	pStructErrorTable pTemp = vws_http_error::GetErrorMsgPtr(nIndex);
	char* pszStatus = pTemp->pszStatusCode;
	char* pszReason = pTemp->pszComment;
	snprintf(pszMsg, PATH_MAX, fmt, pszStatus, pszReason);
	size_t nLength = strlen(pszMsg);
	if (nSize > nLength) {
		memset(pszBuffer, 0, nLength);
		strncpy(pszBuffer, pszMsg, nLength);
		return true;
	} else {
		return false;
	}
}
bool vws_http_error::get_status_buffer(int index, char* buffer, size_t size) {
	pStructErrorTable pTemp = vws_http_error::GetErrorMsgPtr(index);
	char* pszStatus = pTemp->pszStatusCode;
	char* pszReason = pTemp->pszComment;
	snprintf(buffer, size, "HTTP %s %s \r\n", pszStatus, pszReason);
	return true;
}
pStructErrorTable vws_http_error::GetErrorMsgPtr(int nIndex) {
	static struct structErrorTable pErrorList[] = { { 100, "Continue", "100" },
			{ 101, "Switching Protocols", "101" }, { 200, "OK", "200" }, { 201,
					"Created", "201" }, { 202, "Accepted", "202" }, { 203,
					"Non-Authoritative Information", "203" }, { 204,
					"No Content", "204" }, { 205, "Reset Content", "205" }, {
					206, "Partial Content", "206" }, { 300, "Multiple Choices",
					"300" }, { 301, "Moved Permanently", "301" }, { 302,
					"Found", "302" }, { 303, "See Other", "303" }, { 304,
					"Not Modified", "304" }, { 305, "Use Proxy", "305" }, {
					307, "Temporary Redirect", "307" }, { 400, "Bad Request",
					"400" }, { 401, "Unauthorized", "401" }, { 402,
					"Payment Required", "402" }, { 403, "Forbidden", "403" }, {
					404, "Not Found", "404" }, { 405, "Method Not Allowed",
					"405" }, { 406, "Not Acceptable", "406" }, { 407,
					"Proxy Authentication Required", "407" }, { 408,
					"Request Time-out", "408" }, { 409, "Conflict", "409" }, {
					410, "Gone", "410" }, { 411, "Length Required", "411" }, {
					412, "Precondition Failed", "412" }, { 413,
					"Request Entity Too Large", "413" }, { 414,
					"Request-URI Too Large", "414" }, { 415,
					"Unsupported Media Type", "415" }, { 416,
					"Requested range not satisfiable", "416" }, { 417,
					"Expectation Failed", "417" }, { 500,
					"Internal Server Error", "500" }, { 501, "Not Implemented",
					"501" }, { 502, "Bad Gateway", "502" }, { 503,
					"Service Unavailable", "503" }, { 504, "Gateway Time-out",
					"504" }, { 505, "HTTP Version not supported", "505" },
			{ 0 }, };
	for (int i = 0; pErrorList[i].nIndex != 0; i++) {
		if (pErrorList[i].nIndex == nIndex) {
			return &pErrorList[i];
		}
	}
	return NULL;
}
}
